#include"VPU.h"
#include"../../instruction_module/instruction.h"

// void VPU::input_instr(int src1, int src2, int src3, int dst) {
//     this->src_1 = src1;
//     this->src_2 = src2;
//     this->src_3 = src3;
//     this->dst = dst;
// }

// void VPU::Execute() {
//     //读取向量寄存器的值
//     std::vector<double>* src1_reg_value = this->RF->get_vec_register_value(this->src_1);
//     std::vector<double>* src2_reg_value = this->RF->get_vec_register_value(this->src_2);
//     std::vector<double>* src3_reg_value = this->RF->get_vec_register_value(this->src_3);
//     std::vector<double>* dst_reg_value = new std::vector<double>(16, 0.0);

//     //将向量寄存器分为16个64位的值，传入给16个VPE

//     //将VPE的返回值回收，并整合到dst寄存器内部

//     //将dst的值存回向量寄存器堆


//     delete dst_reg_value;
// }

void VPU::Execute_VMAC1(instruction* vmac_instr) {
    std::vector<uint64_t>* res_reg_vec = new std::vector<uint64_t>(16, 0.0);
    write_req_info_reg buffer;
    std::cout << "Executing VPU VMAC1" << std::endl;
    for (int i = 0; i < 16; i++) {
        buffer = this->VPEs.at(i)->Execute_VMAC1(vmac_instr);
        //结果整合为一个16*64的向量寄存器值
        res_reg_vec->at(i) = buffer.value;
    }
    //此时使用buffer内部保留的最后一次的值，去填充write request
    //提交向量寄存器写入请求
    this->WCM->post_write_req(buffer.reg_id, res_reg_vec, buffer.delay);
}

void VPU::Execute_VMAC2(instruction* vmac_instr) {
    std::vector<uint64_t>* res_reg_vec = new std::vector<uint64_t>(16, 0.0);
    write_req_info_reg buffer;
    for (int i = 0; i < 16; i++) {
        buffer = this->VPEs.at(i)->Execute_VMAC2(vmac_instr);
        //结果整合为一个16*64的向量寄存器值
        res_reg_vec->at(i) = buffer.value;
    }
    //此时使用buffer内部保留的最后一次的值，去填充write request
    //提交向量寄存器写入请求
    this->WCM->post_write_req(buffer.reg_id, res_reg_vec, buffer.delay);
}

void VPU::Execute_VMAC3(instruction* vmac_instr) {
    std::vector<uint64_t>* res_reg_vec = new std::vector<uint64_t>(16, 0.0);
    write_req_info_reg buffer;
    for (int i = 0; i < 16; i++) {
        buffer = this->VPEs.at(i)->Execute_VMAC3(vmac_instr);
        //结果整合为一个16*64的向量寄存器值
        res_reg_vec->at(i) = buffer.value;
    }
    //此时使用buffer内部保留的最后一次的值，去填充write request
    //提交向量寄存器写入请求
    this->WCM->post_write_req(buffer.reg_id, res_reg_vec, buffer.delay);
}

void VPU::Execute_VIEU(instruction* vieu_instr) {
    std::vector<uint64_t>* res_reg_vec = new std::vector<uint64_t>(16, 0.0);
    write_req_info_reg buffer;
    for (int i = 0; i < 16; i++) {
        buffer = this->VPEs.at(i)->Execute_VIEU(vieu_instr);
        //结果整合为一个16*64的向量寄存器值
        res_reg_vec->at(i) = buffer.value;
    }
    //此时使用buffer内部保留的最后一次的值，去填充write request
    //提交向量寄存器写入请求
    this->WCM->post_write_req(buffer.reg_id, res_reg_vec, buffer.delay);
}